<?php

namespace app\api\controller;

use app\admin\model\product\Lists;
use app\admin\model\user\Returns;
use app\common\controller\Api;
use think\Request;

class Userreturns extends Api
{
    protected static $model = null;
    protected $noNeedLogin = [''];
    protected $noNeedRight = '*';

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        self::$model = new Returns();
    }

    /**
     * 获取商品清单表
     */
    public function index()
    {
        $pageSize = $this->request->post('page_size', 15);
        $userId = $this->auth->id;
        $where = [
            'user_id' => $userId
        ];

        $lists = self::$model
            ->where($where)
            ->order('id desc')
            ->paginate($pageSize);

        foreach ($lists as &$val) {
            $pInfo = Lists::where('id', $val['product_id'])->find();
            $val['name'] = $pInfo['name'];
//            $val['cover_ico'] = make_full_url($pInfo['image']);
//            $val['intro'] = $pInfo['intro'];
        }

        $this->success('归还列表！', $lists);
    }

    /**
     * 社区管理员申请
     */
    public function create()
    {
        $id = input('id');
        $num = input('num');
        $reply_image = input('reply_image');
        $reply_remark = input('reply_remark');
        if (!$id || !$num || !$reply_image) {
            $this->error('参数缺失！');
        }
        $userId = $this->auth->id;
        $pInfo = \app\admin\model\user\Lists::where('id', $id)
            ->find();
        if (!$pInfo) {
            $this->error('仓库中无该物资！');
        }
        if ($pInfo->type != 2) {
            $this->error('该物资不用归还！');
        }
        if ($pInfo->stock < $num) {
            $this->error('仓库中库存不足！');
        }

        $rdata = [
            'community_id' => $pInfo->community_id,
            'product_id' => $pInfo->product_id,
            'num' => $num,
            'user_id' => $userId,
            'reply_image' => $reply_image,
            'reply_remark' => $reply_remark,
            'pay_time' => time(),
            'status' => 1
        ];

        $ret = Returns::create($rdata);
        if (!$ret) {
            $this->error('申请失败！');
        }

        $this->success('申请成功！');
    }
}